package cn.zn.modules.pz.cur.controller;

import cn.zn.framework.common.utils.R;
import cn.zn.framework.common.utils.Result;
import cn.zn.modules.pz.cur.entity.dto.PzBudgetDTO;
import cn.zn.modules.pz.cur.service.TpMainService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("pz/cur/pzbudget")
public class TpBudgetController {
    @Autowired
    private TpMainService tpMainService;

    /**
     * 某单据预算列表
     *
     * @param mainId
     * @return
     */
    @GetMapping("list/{mainId}")
    public Result list(@PathVariable String mainId) {
        return Result.ok(tpMainService.findBudgetByMainId(mainId));
    }

    /**
     * 预算详情
     *
     * @param id
     * @param mainId
     * @return
     */
    @GetMapping("info/{id}/{mainId}")
    public R info(@PathVariable String id, @PathVariable String mainId) {
        return R.ok().put("budget", tpMainService.findBudgetByIdAndMainId(id, mainId));
    }

    @PostMapping("budget")
    public R save(@Valid @RequestBody PzBudgetDTO pzBudgetDTO) {
        tpMainService.saveBudget(pzBudgetDTO);
        return R.ok();
    }

    @DeleteMapping("budget/{mainId}")
    public R delete(@PathVariable("mainId") String mainId, @RequestBody String[] ids) {
        tpMainService.deleteBudget(mainId, Lists.newArrayList(ids));
        return R.ok();
    }
}
